package com.cskaoyan.controller;

import com.cskaoyan.vo.BaseRespVo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * cookie和session
 * @author stone
 * @date 2022/10/07 15:17
 */
@RestController
public class OtherParameterController {

    @RequestMapping("cookies")
    public BaseRespVo cookies(HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            System.out.println(cookie.getName() + "  -->  " + cookie.getValue());
        }

        return BaseRespVo.ok();
    }

    // session1请求中向session中放入数据
    @RequestMapping("session1")
    public BaseRespVo session1(HttpServletRequest request, String username) {
        HttpSession session = request.getSession();
        session.setAttribute("username",username);
        return BaseRespVo.ok();
    }
    // session2请求中从session中取出数据
    @RequestMapping("session2")
    public BaseRespVo session2(HttpSession session) {
        String username = (String) session.getAttribute("username");

        return BaseRespVo.ok(username);
    }
}
